package org.gruposp2p.dnie.ui.panels;

import dniegp2pclient.DNIeGP2PClientApp;
import java.util.ArrayList;
import java.util.List;
import org.gruposp2p.dnie.model.DNIeSignedDocumentComment;
import org.gruposp2p.dnie.model.DNIeDocumentToSign;
import org.gruposp2p.dnie.model.DNIeDocumentType;
import org.gruposp2p.dnie.model.DNIeSignedDocument;
import org.gruposp2p.dnie.ui.dialogs.OptionsErrorDialog;

/**
 *
 * @author jj
 */
public class SignatureDocumentPanel extends javax.swing.JPanel {

    private DNIeDocumentToSign documentToSign;

    /** Creates new form SignatureDocumentPanel */
    public SignatureDocumentPanel() {
        initComponents();
        signatureDocumentTextPane.setEditable(false);
        signatureResultPanel.setVisible(false);
    }

    public void setDNIeDocumentToSign (DNIeDocumentToSign documentToSign) {
        this.documentToSign = documentToSign;
        signatureDocumentTextPane.setText(documentToSign.getContent());
        titleLabel.setText(documentToSign.getTitle());
        switch (documentToSign.getSignatureDataType()) {
            case MANIFEST:
                optionsPanel.setVisible(false);
                break;
            case VOTE:
                optionsPanel.repaintOptions(documentToSign.getDNIeDocumentToSignoptionCollection(),
                        documentToSign.getOptionSelectionType());
                optionsPanel.setVisible(true);
                break;
        }
        signatureResultPanel.setVisible(false);
        commentPanel.setSigned(false);
        commentPanel.setCommentText("");
    }

    public void showSignedState (boolean isSigned, String xmlSignedDocument,
            String signedDocumentTitle) {
        if (isSigned) {
            signatureResultPanel.setVisible(true);
            signatureResultPanel.setSignatureOK(xmlSignedDocument,
                    documentToSign.getTitle());
        }
        if (documentToSign.getSignatureDataType().equals(DNIeDocumentType.VOTE)) {
            this.optionsPanel.setSigned(isSigned);
        }
        commentPanel.setSigned(isSigned);
    }

    public void showSigningState (boolean isSigning) {
        commentPanel.setSigned(isSigning);
    }

    public DNIeSignedDocument getDNIeSignedDocument () {
        DNIeSignedDocument dnieSignedDocument = new DNIeSignedDocument();
        dnieSignedDocument.setDnieDocumentToSignId(documentToSign);
        dnieSignedDocument.setDNIeSignedDocumentOptionSelectedCollection(
                optionsPanel.getOptions());
        List<DNIeSignedDocumentComment> comments = new ArrayList<DNIeSignedDocumentComment>();
        comments.add(commentPanel.getComment());
        dnieSignedDocument.setDNIeSignedDocumentCommentCollection(comments);
        return dnieSignedDocument;
    }

    public boolean isDataValid () {
        boolean result = false;
        if (documentToSign.getSignatureDataType().equals(DNIeDocumentType.VOTE)) {
            if (!optionsPanel.isDataValid()) {
                OptionsErrorDialog optionsErrorDialog = new OptionsErrorDialog (
                    DNIeGP2PClientApp.getApplication().getMainFrame() , true);
                optionsErrorDialog.setVisible(true);
            } else result = true;
        } else result = true;
        return result;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        commentPanel = new org.gruposp2p.dnie.ui.panels.CommentPanel();
        optionsPanel = new org.gruposp2p.dnie.ui.panels.OptionsPanel();
        contentPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        signatureDocumentTextPane = new javax.swing.JTextPane();
        signatureResultPanel = new org.gruposp2p.dnie.ui.panels.SignatureResultPanel();
        titleLabel = new javax.swing.JLabel();

        commentPanel.setName("commentPanel"); // NOI18N
        commentPanel.setPreferredSize(new java.awt.Dimension(504, 300));

        optionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Opciones"));
        optionsPanel.setName("optionsPanel"); // NOI18N
        optionsPanel.setPreferredSize(new java.awt.Dimension(39, 100));

        contentPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Contenido")); // NOI18N
        contentPanel.setName("contentPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        signatureDocumentTextPane.setBackground(new java.awt.Color(255, 255, 255));
        signatureDocumentTextPane.setName("signatureDocumentTextPane"); // NOI18N
        signatureDocumentTextPane.setPreferredSize(new java.awt.Dimension(6, 300));
        jScrollPane1.setViewportView(signatureDocumentTextPane);

        javax.swing.GroupLayout contentPanelLayout = new javax.swing.GroupLayout(contentPanel);
        contentPanel.setLayout(contentPanelLayout);
        contentPanelLayout.setHorizontalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                .addContainerGap())
        );
        contentPanelLayout.setVerticalGroup(
            contentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );

        signatureResultPanel.setName("signatureResultPanel"); // NOI18N

        titleLabel.setFont(new java.awt.Font("DejaVu Sans", 1, 15)); // NOI18N
        titleLabel.setText(" ");
        titleLabel.setName("titleLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(signatureResultPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(contentPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(commentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
            .addComponent(optionsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(signatureResultPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(optionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(commentPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.gruposp2p.dnie.ui.panels.CommentPanel commentPanel;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private org.gruposp2p.dnie.ui.panels.OptionsPanel optionsPanel;
    private javax.swing.JTextPane signatureDocumentTextPane;
    private org.gruposp2p.dnie.ui.panels.SignatureResultPanel signatureResultPanel;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables

 }
